Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

dash-get

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dash-get

A tiny get function, similar to Lodash.get

  • 1.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
210K
decreased by-0.71%
Maintainers
1
Weekly downloads
 
Created
Source

✊ (dash) get

Build Status npm version

A tiny get function', similar to Lodash.get

✨ Features

  • Zero dependencies!
  • Super tiny, at ~200 bytes gzipped
  • Works almost exactly like Lodash.get
  • Ultra speedy! Check out the performance tests

🔧 Installation

Add dash-get to your project via npm install:

npm install --save dash-get

🕹 Usage

You can easily retrieve a value from a (deeply) nested object with dash-get, like so:

import get from 'dash-get'

const someObject = {...}

const deeplyNestedValue = get(someObject, 'the.path.to.the.nested.value')
// value

The path could also be an Array:

const someObject = {...}

const deeplyNestedValue = get(someObject, ['the', 'path', 'to', 'the', 'nested', 'value'])
// value

🎬 API

get(obj, path, fallback)
ArgumentTypeDescription
objObjectThe object to get the value from.
pathArray<string>/stringThe path to the value.
fallbackanyThe fallback value, in case the desired value could not be retrieved.

👻 Unsupported feature

This module does not support this particular use case:

get(object, 'a[0].b.c')

🤔 Why an npm module tho?

You totally don't have to npm install this. This exists for convenience purposes 😊.

In fact, it's encouraged that you add the get code to your code base! One less depenency to install and manage.

Here it is!

function get(obj, path, fallback) {
  if (!obj || !path) return fallback;
  const paths = Array.isArray(path) ? path : path.split(".");
  let results = obj;
  let i = 0;

  while (i < paths.length && results !== undefined && results !== null) {
    results = results[paths[i]];
    i++;
  }

  if (i === paths.length) {
    return results !== undefined ? results : fallback;
  }

  return results !== undefined && results !== null ? results : fallback;
}

❤️ Thanks

Thanks to @knicklabs for pairing with me on this one!

Keywords

FAQs

Package last updated on 09 Jan 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc